using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(HMI_Slider), true)]
public class HMI_Slider_Editor : HMI_EditorBase
{
    private HMI_Slider mTarget;
    private MyText mText;
    private bool foldoutText = false;
    private MyTextEditor textEditor;

    private LPImage mIcon;
    private bool foldoutIcon = false;
    private LPImageEditor imageEditor;

    private Sprite curIcon;

    private bool hasFrameAni;
    public override void Awake()
    {
        base.Awake();
        mTarget = target as HMI_Slider;
        mText = mTarget.transform.Find("Text").GetComponent<MyText>();
        mIcon = mTarget.transform.Find("Icon").GetComponent<LPImage>();

        curIcon = mIcon.sprite;

        hasFrameAni = (mIcon.GetComponent<SingleFrameAni>() != null);
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUI.BeginChangeCheck();
        mText.text = EditorGUILayout.TextField("文字：", mText.text);
        if (textEditor == null)
            textEditor = CreateEditor(mText) as MyTextEditor;
        foldoutText = textEditor.CreateStyleEditor(foldoutText);
        
        curIcon = (Sprite)EditorGUILayout.ObjectField("图片：", curIcon, typeof(Sprite),false);

        var targetSp = mIcon.gameObject.GetComponent<Image>().sprite;
        if (HMI_Slider_Editor.isFrameAniName(targetSp))
            hasFrameAni = EditorGUILayout.Toggle("使用帧动画：", hasFrameAni);
        if (imageEditor == null)
            imageEditor = CreateEditor(mIcon) as LPImageEditor;
        foldoutIcon = imageEditor.CreateStyleEditor(foldoutIcon);
        
        if (EditorGUI.EndChangeCheck())
        {
            mIcon.sprite = curIcon;
            mIcon.SetNativeSize();
            mTarget.SetVoiceText(mText.text);
            EditorUtility.SetDirty(mTarget);

            RefreshFrameAni();
        }
    }
    protected override string GetDataJson()
    {
        var data = new HMI_SliderData();
        data.TextData = mText.GetData();
        data.IconData = mIcon.GetData(AssetDatabase.GetAssetPath(curIcon)); 
        data.HasFrameAni = hasFrameAni;

        string json = JsonUtility.ToJson(data, true);
        return json;
    }

    protected override void SetData(string json)
    {
        HMI_SliderData data = JsonUtility.FromJson<HMI_SliderData>(json);
        
        if (mText != null && data.TextData != null)
        {
            MyTextData textData = data.TextData;
            mText.SetData(textData);
        }
        if (mIcon != null && data.IconData != null)
        {
            LPImageData imageData = data.IconData;
            mIcon.SetData(imageData);

            hasFrameAni = data.HasFrameAni;
            if (hasFrameAni)
                RefreshFrameAni();
        }

        base.SetData(json);
        EditorUtility.SetDirty(mTarget);
    }

    private void RefreshFrameAni()
    {
        var iconCom = mIcon.GetComponent<SingleFrameAni>();
        if (hasFrameAni)
        {
            if (iconCom == null)
            {
                iconCom = mIcon.gameObject.AddComponent<SingleFrameAni>();
            }
            iconCom.SetSprite();
        }
        else
        {
            if (iconCom != null)
            {
                DestroyImmediate(iconCom);
            }
        }
    }

}
[Serializable]
public class HMI_SliderData
{
    public MyTextData TextData;  //文字
    public LPImageData IconData;  //图片
    public bool HasFrameAni;  //是否有帧动画
}
